package com.zh.rxharmony.sample.slice;

import com.zh.rxbus.RxBus;
import com.zh.rxharmony.ResourceTable;
import com.zh.rxharmony.sample.util.ToastUtil;
import com.zh.rxharmony.sample.util.rx.RxSchedulerUtil;
import com.zh.rxharmony.sample.util.rx.RxUtil;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.webengine.Navigator;
import ohos.agp.components.webengine.ResourceRequest;
import ohos.agp.components.webengine.WebAgent;
import ohos.agp.components.webengine.WebView;

import java.util.concurrent.TimeUnit;

public class MainAbilitySlice extends AbilitySlice {
    private Disposable mDisposable;
    private Text vText;
    private Button vButton;
    private WebView vWebView;

    private boolean isCountDown;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        findView();
        bindView();
    }

    private void findView() {
        vText = (Text) findComponentById(ResourceTable.Id_text);
        vButton = (Button) findComponentById(ResourceTable.Id_button);
        vWebView = (WebView) findComponentById(ResourceTable.Id_web_view);
    }

    private void bindView() {
        vButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (isCountDown) {
                    return;
                }
                vButton.setVisibility(Component.HIDE);
                //60秒后过期
                int expireTime = 60;
                mDisposable = RxUtil.countdown(0, 1, expireTime, TimeUnit.SECONDS)
                        .compose(RxSchedulerUtil.ioToMain())
                        .subscribe(new Consumer<Integer>() {
                            @Override
                            public void accept(Integer time) throws Exception {
                                vText.setText(time + "s");
                                if (time == 0) {
                                    isCountDown = false;
                                    ToastUtil.toast(getContext(), "到时间啦！");
                                    vButton.setVisibility(Component.VISIBLE);
                                } else {
                                    isCountDown = true;
                                }
                                //发送事件
                                RxBus.getDefault().post(time, "countDown");
                            }
                        });
            }
        });
        vWebView.getWebConfig().setJavaScriptPermit(true);
        vWebView.setWebAgent(new WebAgent() {
            @Override
            public boolean isNeedLoadUrl(WebView webView, ResourceRequest request) {
                //直接交由当前WebView加载，而不是跳转到外部浏览器
                return true;
            }
        });
        vWebView.load("https://www.baidu.com");
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    protected void onBackPressed() {
        Navigator navigator = vWebView.getNavigator();
        if (navigator.canGoBack()) {
            navigator.goBack();
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mDisposable != null && mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
